package service.Impl;

import dao.mapper.CartLineMapper;
import org.apache.ibatis.session.SqlSession;
import service.ICartLineService;
import utils.SqlSessionUtil;
import vo.Book;
import vo.Cart;
import vo.CartLine;

import java.util.ArrayList;
import java.util.List;

public class ICartLineServiceImpl implements ICartLineService {
    SqlSession sqlSession=new SqlSessionUtil().sqlSession;
    CartLineMapper cartLineMapper=sqlSession.getMapper(CartLineMapper.class);
    @Override
    public boolean addCartLine(int userId,int bookId) {

        ICartServiceImpl cartService=new ICartServiceImpl();
        Cart cart=cartService.selectCart(userId);
        int cartId=cart.getId();
        CartLine cartLine=selectCartLine(cartId,bookId);

        //先判断库存是否够
        IBookServiceImpl bookService=new IBookServiceImpl();
        Book book=bookService.selectBookById(bookId);
        int inventory=book.getInventory();
        List<CartLine>cartLineList=cartLineMapper.selectCartLineByBookId(bookId);
        int totalCount=totalCount(cartLineList);
        if(inventory<=totalCount){
            //添加失败
            return false;
        }

        if(cartLine==null){
            //如果之间没有添加过这本书
            CartLine cartLine1=new CartLine();
            cartLine1.setCartId(cartId);
            cartLine1.setBookId(bookId);
            cartLine1.setQuanlity(1);
            cartLineMapper.addCartLine(cartLine1);



            sqlSession.commit();
        }else {
            //如果之前添加过这本书

            int quanlity=cartLine.getQuanlity();
            int newQuan=quanlity+1;
            cartLineMapper.update(cartId,bookId,newQuan);
            sqlSession.commit();

        }
        //改变购物车总价格

        List<CartLine>cartLines=cartLineMapper.selectCartLineByCartId(cartId);
        double totalPrice=totalPrice(cartLines);
        cartService.changeTotalPrice(cartId,totalPrice);
        return true;
    }

    @Override
    public void cutCartLine(int userId, int bookId) {

        ICartServiceImpl cartService=new ICartServiceImpl();
        Cart cart=cartService.selectCart(userId);
        int cartId=cart.getId();

        CartLine cartLine=selectCartLine(cartId,bookId);
        if(cartLine==null){
            return ;
        }else if(cartLine.getQuanlity()==1){
            //只有一本书，直接删除数据
            cartLineMapper.delCartLine(cartId,bookId);


            sqlSession.commit();
        }else{
            //购物车中有很多本，删除一本

            int quanlity=cartLine.getQuanlity();
            int newQuan=quanlity-1;
            cartLineMapper.update(cartId,bookId,newQuan);
            sqlSession.commit();
        }

        //改变购物车总价格
        List<CartLine>cartLines=cartLineMapper.selectCartLineByCartId(cartId);
        double totalPrice=totalPrice(cartLines);
        cartService.changeTotalPrice(cartId,totalPrice);

    }

    @Override
    public List<CartLine> selectCartLineByCartId(int cartId) {
        List<CartLine>cartLines=new ArrayList<>();
        cartLines=cartLineMapper.selectCartLineByCartId(cartId);
        return cartLines;
    }


    @Override
    public CartLine selectCartLine(int cartId, int bookId) {
        CartLine cartLine=new CartLine();
        cartLine=cartLineMapper.selectCartLine(cartId,bookId);
        return cartLine;
    }

    @Override
    public double totalPrice(List<CartLine> cartLines) {
        IBookServiceImpl bookService=new IBookServiceImpl();
        double totalPrice=0.00;
        for(CartLine cartLine:cartLines){
            int bookId=cartLine.getBookId();
            Book book=bookService.selectBookById(bookId);
            double price=book.getPrice();
            int num=cartLine.getQuanlity();
            totalPrice=(price*num)+totalPrice;
        }
        return totalPrice;
    }

    @Override
    public int totalCount(List<CartLine> cartLines) {
        int totalCount=0;
        for(CartLine cartLine:cartLines){
            totalCount=cartLine.getQuanlity()+totalCount;
        }
        return totalCount;
    }

    @Override
    public CartLine selectCartLineById(int cartLineId) {
        if(cartLineId!=0){
            CartLine cartLine=cartLineMapper.selectCartLineById(cartLineId);
            return cartLine;
        }else {
            return null;
        }
    }


    @Override
    public void delCartLineById(int id) {
        if(cartLineMapper.selectCartLineById(id)==null){
            return;
        }else {
            cartLineMapper.delCartLineById(id);
            sqlSession.commit();
        }

    }

}
